package com.lagou.edu.controller;

import com.lagou.edu.service.UserService;
import com.lagou.edu.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 1.注册
 * 2.登录
 * 3.查询用户信息
 * 4.退出
 * @date 2020/5/26 20:42
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/login/{email}/{password}")
    public Result login(@PathVariable String email, @PathVariable String password, HttpServletResponse response){
       return userService.login(email, password, response);
    }

    @GetMapping("/register/{email}/{password}/{code}")
    public Result register(@PathVariable("email") String email, @PathVariable("code") String code, @PathVariable("password") String password, HttpServletResponse httpServletResponse){
        return userService.register(email, password, code, httpServletResponse);
    }

    @GetMapping("/isRegistered/{email}")
    public Boolean isRegistered(@PathVariable String email) {
        return userService.isRegistered(email);
    }

    @GetMapping("/info/{token}")
    public Result userInfo(@PathVariable String token) {
        return userService.queryUserInfoByToken(token);
    }

    @GetMapping("/logout")
    public void logout(HttpServletRequest httpServletRequest){
        Cookie[] cookies = httpServletRequest.getCookies();
        for(Cookie cookie : cookies){
            cookie.setMaxAge(0);
        }
    }

}
